import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
import router from './router'
import store from './store'
import http from 'axios'
import './api/mock.js'
import Cookie from 'js-cookie'

Vue.use(ElementUI);
Vue.prototype.$http = http

Vue.config.productionTip = false

router.beforeEach((to,from,next) => {
  const token = Cookie.get('token')
  // store.commit('getToken')
  // const token = store.state.user.token
  if(!token && to.name !== 'login') {
    next({name: 'login'})
  } else if(token && to.name === 'login') {
    next({name: 'home'})
  } else {
    next()
  }
})

new Vue({
  router,
  store,
  created() {
    store.commit('addMenu', router)
  },
  render: h => h(App),
}).$mount('#app')
